// 动态类型
// generics

/* function numGen(a: number, b: number):Array<number> {
  return [a, b]
}

function strGen(a: string, b: string): Array<string> {
  return [a, b]
} */

function typeGen<T>(a:T, b:T): Array<T> {
  return [a, b]
}

// number
typeGen(1, 2)

// string
typeGen("1", "2")

// 类型别名
type AGen<T> = string | number | T

let aGenNum:AGen<number> = 1
let aGenStr:AGen<string> = "1"
let aGenBoolean: AGen<Boolean> = false

interface Data<T> {
  msg: T
}

let dataGen:Data<string> = {
  // msg: 1
  msg: "1"
}

/* function addGen<T, K>(a:T, b:K):Array<T | K> {
  return [a, b]
} */

function addGen<T = number, K = string>(a: T, b: K): Array<T | K> {
  return [a, b]
}

addGen(1, 2)
addGen(1, false)
// addGen()

const axios = {
  get<T>(url:string):Promise<T> {
    return new Promise((reslove, reject) => {
      let xhr:XMLHttpRequest = new XMLHttpRequest()
      xhr.open("GET", url)
      xhr.onreadystatechange = () => {
        if(xhr.readyState === 4 && xhr.status === 200) {
          reslove(JSON.parse(xhr.responseText))
        }
      }
      xhr.send(null)
    })
  }
}

interface DataAxios {
  code: number,
  msg: string
}

axios.get<DataAxios>("./data.json").then(res => {
  console.log(res.code)
  console.log(res.msg)
})

/* 
因为node环境没有xhr
所以只执行：tsc 编译下文件即可
然后新建一个html文件，引入编译后的js文件即可
*/
